# -*- coding: utf-8 -*-
# @Time    : 2019-05-31 15:00
# @Author  : ken
'''
协程池装饰器
'''
import sys
print(sys.path)
from functools import wraps
# import gevent.pool
from gevent.pool import Pool

class GeventTomorrow():
    def __init__(self, future, timeout):
        self._future = future
        self._timeout = timeout
        self._wait = self._future.result

    def __getattr__(self, name):
        result = self._future.result(self._timeout)
        return result.__getattribute__(name)


def asyncs(n, gevent_Pool, timeout=None):
    def decorator(f):
        '''验证传参'''
        if isinstance(n, int):
            pool = gevent_Pool(n)
        elif isinstance(n, gevent_Pool):
            pool = n
        else:
            raise TypeError("Invalid type: %s" % type(gevent_Pool))

        @wraps(f)
        def wrapped(*args, **kwargs):
            return GeventTomorrow(pool.map(f, *args, **kwargs), timeout=timeout)

        return wrapped

    return decorator


def gevent_threads(n, timeout=None):
    return asyncs(n, Pool, timeout)
